package kr.hikt.androidcomplete01.ch07;

import kr.hikt.androidcomplete01.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.os.Bundle;
import android.view.View;

public class MirrorImage extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new MyView(this));
	}
	
	class MyView extends View {
		private Paint pnt;
		private Bitmap car;
		private Bitmap mirror;
		private Bitmap mirror2;
		private Matrix matrix;
		private ComposeShader comp;
		private int width;
		private int height;
		
		public MyView(Context context) {
			super(context);
			pnt = new Paint();
			car = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.soyou);
			width = car.getWidth();
			height = car.getHeight();
			matrix = new Matrix();
			matrix.preScale(1, -1f);
			mirror = Bitmap.createBitmap(car, 0, 0, width, height, matrix, false);
			matrix.preScale(-1f, 1);
			mirror2 = Bitmap.createBitmap(car, 0, 0, width, height, matrix, false);
			comp = new ComposeShader(
					new BitmapShader(mirror, TileMode.REPEAT, TileMode.REPEAT),
					new LinearGradient(0, height, 0, height + height, Color.TRANSPARENT, Color.BLACK, TileMode.REPEAT),
					new PorterDuffXfermode(PorterDuff.Mode.DARKEN));
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			canvas.drawColor(Color.BLACK);
			
			canvas.drawBitmap(car, 0, 0, null);
			pnt.setShader(comp);
			canvas.drawRect(0, height, width, height + height, pnt);
		}
	}
}
